package com.shareware.common.toast

import android.content.Context
import android.view.Gravity
import android.view.View
import android.widget.TextView
import android.widget.Toast
import androidx.annotation.LayoutRes
import com.shareware.common.R

/**
 * desc: 功能描述
 * email: mobiledeveloper@qq.com
 */

/**
 * 屏幕居中显示吐司
 * @param gravity 对齐方式
 * @param layout 吐司布局, 要求消息内容TextView的ID使用[@android:id/message]
 * @param xOffset x轴偏移量
 * @param yOffset y轴偏移量
 */
class ToastGravityFactory @JvmOverloads constructor(
    private val gravity: Int = Gravity.CENTER,
    @LayoutRes val layout: Int = R.layout.layout_toast_gravity,
    private val xOffset: Int = 0,
    private val yOffset: Int = 0,
) : ToastFactory {

    override fun onCreate(context: Context, message: CharSequence, duration: Int): Toast {
        val toast = Toast.makeText(context, message, duration)
        val view = View.inflate(context, layout, null)
        view.findViewById<TextView>(android.R.id.message).text = message
        toast.view = view
        toast.setGravity(gravity, xOffset, yOffset)
        return toast
    }
}